package com.gorkr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

/**
 * @author 张心宇 <zhangxinyu20@kuaishou.com>
 * Created on 2022-12-16
 */
public class M49GroupAnagrams {
    public List<List<String>> groupAnagrams(String[] strs) {
        ArrayList<Map<Character, Integer>> mapList = new ArrayList<>();
        List<List<String>> res = new ArrayList<>();

        for (String s : strs) {
            char[] chars = s.toCharArray();
            Map<Character, Integer> map = new HashMap<>();
            for (char c : chars) {
                map.put(c, map.getOrDefault(c, 0) + 1);
            }
            int i = 0;
            for (; i < mapList.size(); i++) {
                if (mapList.get(i).equals(map)) {
                    break;
                }
            }
            if (i >= mapList.size()) {
                mapList.add(map);
                ArrayList<String> tmp = new ArrayList<>();
                tmp.add(s);
                res.add(tmp);
            } else {
                res.get(i).add(s);
            }

        }
        return res;

    }

    @Test
    public void test() {
        String[] s = {"eat", "tea", "tan", "ate", "nat", "bat"};
        String[] a = {"a"};
        String[] b = {""};
        String[] c = {"ddddddddddg", "dgggggggggg"};
        System.out.println(groupAnagrams(c));
    }
}
